/**
 * 处理数据库数据的操作
 */
const models = require('../models/init-models')
class ReviewService {
  // 新增书本借阅记录（借书）
  async createReview(review_id, book_id, user_id, rating, content) {
    // 写入数据库
    const res = await models.lib_book_reviews.create({
      // 表的字段
      review_id,
      book_id,
      user_id,
      rating,
      content
    })
    return res
  }

  // 获取借阅记录列表
  async getReviewList() {
    const res = await models.lib_book_reviews.findAll({
      attributes: ['review_id', 'book_id', 'user_id', 'rating', 'content'],
      include: [{ model: models.lib_users, as: 'user', attributes: ['name'] },
      { model: models.lib_books, as: 'book', attributes: ['title'] }]
    })
    return res
  }

}

module.exports = new ReviewService()
